package xgenerator.ui.swing;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import xgenerator.ui.swing.adapter.LogonAdapter;

/**
 * <p>
 * Title: 登录窗口
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: Copyright (c) 2012
 * </p>
 * <p>
 * Company: liss
 * </p>
 * @author <a href="mailto:shushanlee@msn.com">liss</a>
 * @version 1.0
 * @see 
 * @since   1.0
 */
public class LogonFrame extends JFrame implements UIConstants {

	private JLabel userNameLabel;

	private JTextField userNameField;

	private JLabel passwordLabel;

	private JPasswordField passwordField;

	private JButton submitBtn;

	private JButton resetBtn;

	/**
	 * constructor
	 */
	public LogonFrame() {
		this("登录");
	}
	
	/**
	 * constructor
	 * @param title
	 */
	public LogonFrame(String title) {
		super();
		initComponents(title);
	}

	/**
	 * <p>
	 * Title：初始化组件
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 */
	private void initComponents(String title) {
		this.setContentPane(buildContentPane());
		this.getLayeredPane().setLayout(null);
		this.getLayeredPane().add(new BackgroundImageLabel(IMAGE_CLASS_PATH + "loginbg.jpg"), new Integer(Integer.MIN_VALUE));
		
		this.setTitle(title);
		this.setIconImage(IconUtils.createIconImage(APP_ICON_IMAGE));

		Toolkit tk = Toolkit.getDefaultToolkit();
		Dimension screenSize = tk.getScreenSize();
		int screenHeight = screenSize.height;
		int screenWidth = screenSize.width;
		this.setSize(322, 240);
		this.setLocation((screenWidth - getWidth()) / 2, (screenHeight - getHeight() - 30) / 2);
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//		this.setUndecorated(true);  //去掉窗口的装饰
//		this.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
		this.setVisible(true);
	}
	
	/**
	 * <p>
	 * Title：buildContentPane
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @return
	 */
	private JPanel buildContentPane() {
		JPanel panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		panel.setOpaque(false);
		ActionListener logonActionListener = new LogonAdapter(this);
		
		final GridBagLayout gridBagLayout = new GridBagLayout();
//		panel.setLayout(gridBagLayout);
//		panel.setLayout(new GridLayout(3, 2));
		
		this.userNameLabel = new JLabel("用户名:", JLabel.CENTER);
		this.userNameField = new JTextField("admin");
		this.userNameField.setActionCommand(LogonAdapter.USERNAME_COMMAND);
		
		this.passwordLabel = new JLabel("密  码:", JLabel.CENTER);
		this.passwordField = new JPasswordField("admin");
		this.passwordField.setActionCommand(LogonAdapter.PASSWORD_COMMAND);
		
		this.submitBtn = new JButton("登录");
		this.submitBtn.setActionCommand(LogonAdapter.LOGON_COMMAND);
		this.submitBtn.addActionListener(logonActionListener);
		
		this.resetBtn = new JButton("重置");
		this.resetBtn.setActionCommand(LogonAdapter.RESET_COMMAND);
		this.resetBtn.addActionListener(logonActionListener);
		
		
		Box baseBox = Box.createHorizontalBox();
		panel.add(baseBox);
		
		Box userNameBox = Box.createVerticalBox();
		
		userNameBox.add(Box.createRigidArea(new Dimension(100, 50)));
		//Box centerBox = Box.createVerticalBox();
		userNameBox.add(userNameLabel);
		userNameBox.add(userNameField);
		userNameBox.add(Box.createRigidArea(new Dimension(100, 50)));
		baseBox.add(userNameBox);
		
//		centerBox.add(userNameLabel);
//		centerBox.add(userNameField);
//		centerBox.add(Box.createGlue());
//		centerBox.add(passwordLabel);
//		centerBox.add(passwordField);
//		centerBox.add(resetBtn);
//		centerBox.add(submitBtn);
		
		baseBox.add(Box.createHorizontalGlue());
		
		/*
		 * 设置布局管理器参数
		 */
		GridBagConstraints constraints = new GridBagConstraints();
//		int gridx = GridBagConstraints.RELATIVE;  //组件位置X
//		int gridy = GridBagConstraints.RELATIVE;  //组件位置Y
//		int gridwidth = 1;  //组件所占的单位宽度
//		int gridheight = 1;  //组件所占的单位高度
//		double weightx = 0.5;  //当窗口变大时，各组件X跟着变大的比例（0.0表示变化是并不跟随着变大，1.0表示代表大比率变化）
//		double weighty = 0.5;  //当窗口变大时，各组件Y跟着变大的比例（0.0表示变化是并不跟随着变大，1.0表示代表大比率变化）
//		int anchor = GridBagConstraints.CENTER;  //当组件空间大于组件本身时，将组件置于何处(9个方位)CENTER、NORTH、NORTHEAST、EAST、SOUTHEAST、SOUTH、SOUTHWEST、WEST、NORTHWEST
//		int fill = GridBagConstraints.HORIZONTAL;  //当组件所处的位置有剩下的空间时，组件填充空间的方式（NONE、VERTICAL、HORIZONTAL、BOTH）
//		Insets insets = new Insets(5, 5, 5, 5);  //设置组件之间彼此上、下、左、右的间距
//		int ipadx = 0;  //设置组件内的X间距，默认值为0
//		int ipady = 0;  //设置组件内的Y间距，默认值为0

		/*
		 * userNameLabel
		 */
//		gridx = 1;
//		gridy = 1;
//      gridwidth = 200;
//		gridheight = 100;
//		constraints = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, weightx, weighty, anchor, fill, insets, ipadx, ipady);
//		gridBagLayout.setConstraints(this.userNameLabel, constraints);
//      panel.add(userNameField);
        
		/*
		constraints.gridx = 5;
		constraints.gridy = 5;
		constraints.weightx = 50;
		constraints.weighty = 50;
		constraints.gridwidth = 20;
		panel.add(userNameLabel, constraints);
		*/
		
		/*
		 * userNameField
		 */
//		gridx = 2;
//		gridy = 2;
//		gridwidth = 300;
//		gridheight = 100;
//		anchor = GridBagConstraints.NORTH;
//		constraints = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, weightx, weighty, anchor, fill, insets, ipadx, ipady);
//		gridBagLayout.setConstraints(this.userNameField, constraints);
//		panel.add(userNameField);
		
		/*
		constraints.gridx = 0;
		constraints.gridy = 1;
		constraints.weightx = 100;
		constraints.weighty = 100;
		panel.add(userNameField, constraints);
		*/
		
		/*
		 * passwordLabel
		 */
//		gridx = 1;
//		gridy = 0;
//		gridwidth = 100;
//		gridheight = 100;
//		constraints = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, weightx, weighty, anchor, fill, insets, ipadx, ipady);
//		gridBagLayout.setConstraints(this.passwordLabel, constraints);
//		panel.add(passwordLabel);
		
		/*
		constraints.gridx = 1;
		constraints.gridy = 0;
		panel.add(passwordLabel, constraints);
		*/
		
		/*
		 * passwordField
		 */
//		gridx = 1;
//		gridy = 1;
//		gridwidth = 100;
//		gridheight = 100;
//		constraints = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, weightx, weighty, anchor, fill, insets, ipadx, ipady);
//		gridBagLayout.setConstraints(this.passwordField, constraints);
//		panel.add(passwordField);
		
		/*
		constraints.gridx = 1;
		constraints.gridy = 1;
		panel.add(passwordField, constraints);
		*/
		
		/*
		 * submitBtn
		 */
//		gridx = 2;
//		gridy = 0;
//		gridwidth = 100;
//		gridheight = 100;
//		constraints = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, weightx, weighty, anchor, fill, insets, ipadx, ipady);
//		gridBagLayout.setConstraints(this.submitBtn, constraints);
//		panel.add(submitBtn);
		
		/*
		constraints.gridx = 2;
		constraints.gridy = 0;
		panel.add(submitBtn, constraints);
		*/
		
		/*
		 * resetBtn
		 */
//		gridx = 2;
//		gridy = 1;
//		gridwidth = 100;
//		gridheight = 100;
//		constraints = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, weightx, weighty, anchor, fill, insets, ipadx, ipady);
//		gridBagLayout.setConstraints(this.resetBtn, constraints);
//		panel.add(resetBtn);
		
		/*
		constraints.gridx = 2;
		constraints.gridy = 1;
		panel.add(resetBtn, constraints);
		*/
		
		return panel;
	}

	/**
	 * @return the passwordField
	 */
	public JPasswordField getPasswordField() {
		return passwordField;
	}

	/**
	 * @param passwordField the passwordField to set
	 */
	public void setPasswordField(JPasswordField passwordField) {
		this.passwordField = passwordField;
	}

	/**
	 * @return the passwordLabel
	 */
	public JLabel getPasswordLabel() {
		return passwordLabel;
	}

	/**
	 * @param passwordLabel the passwordLabel to set
	 */
	public void setPasswordLabel(JLabel passwordLabel) {
		this.passwordLabel = passwordLabel;
	}

	/**
	 * @return the resetBtn
	 */
	public JButton getResetBtn() {
		return resetBtn;
	}

	/**
	 * @param resetBtn the resetBtn to set
	 */
	public void setResetBtn(JButton resetBtn) {
		this.resetBtn = resetBtn;
	}

	/**
	 * @return the submitBtn
	 */
	public JButton getSubmitBtn() {
		return submitBtn;
	}

	/**
	 * @param submitBtn the submitBtn to set
	 */
	public void setSubmitBtn(JButton submitBtn) {
		this.submitBtn = submitBtn;
	}

	/**
	 * @return the userNameField
	 */
	public JTextField getUserNameField() {
		return userNameField;
	}

	/**
	 * @param userNameField the userNameField to set
	 */
	public void setUserNameField(JTextField userNameField) {
		this.userNameField = userNameField;
	}

	/**
	 * @return the userNameLabel
	 */
	public JLabel getUserNameLabel() {
		return userNameLabel;
	}

	/**
	 * @param userNameLabel the userNameLabel to set
	 */
	public void setUserNameLabel(JLabel userNameLabel) {
		this.userNameLabel = userNameLabel;
	}

}
